package com.tutu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tutu.reggie.common.CustomException;
import com.tutu.reggie.dto.DishDto;
import com.tutu.reggie.entity.Dish;
import com.tutu.reggie.entity.DishFlavor;
import com.tutu.reggie.entity.Setmeal;
import com.tutu.reggie.mapper.DishMapper;
import com.tutu.reggie.service.DishFlavorService;
import com.tutu.reggie.service.DishService;
import com.tutu.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增菜品时，同时插入菜品对应的口味数据 需要操作两张表 dish,dish_flvor
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存对用菜品的基本信息到dish表
        this.save(dishDto);

        Long dishDtoId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDtoId);
            return item;
        }).collect(Collectors.toList());

        //保存对应菜品口味到dish_flavor
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 根据id查询菜品信息以及对应的口味信息
     *
     * @param id
     * @return
     */
    public DishDto getByIdWithFlvor(Long id) {
        //查询菜品的基本信息，从dish表查询
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }


    /**
     * 修改菜品时，同时修改口味表
     *
     * @param dishDto
     */
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //先清理当前菜品对应口味表中的数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 删除菜品时  检查菜品所关联的套餐是否正在起售以及对应的口味表
     *
     * @param ids
     */
    public void deleteWithCheck(Long[] ids) {
        Set<Long> set = new HashSet<>();
        //1.检查所选的菜品  是否有关联的正在起售的套餐
        for (Long id : ids) {
            List<Long> setmealIds = findSetmealIdIdBydishId(id);
            if (setmealIds.size() != 0) {
                List<Setmeal> sl = new ArrayList<>();
                for (Long setmealId : setmealIds) {
                    Setmeal byId = setmealService.getById(setmealId);
                    sl.add(byId);
                }
                if (sl.size() != 0) {
                    throw new CustomException("关联了正在起售的套餐,无法删除");
                }
            }
        }
        List<Long> dishIds = Arrays.asList(ids);
        //2.1 若没有关联套餐 删除dish表中的数据
        this.removeByIds(dishIds);

        //2.2 删除对应口味表中的数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId, dishIds);
        dishFlavorService.remove(queryWrapper);
    }

    /**
     * 根据DishId查询对应的SetMealId
     *
     * @param dishId
     * @return
     */
    public List<Long> findSetmealIdIdBydishId(Long dishId) {
        return dishMapper.findSetmealIdIdBydishId(dishId);

    }
}
